package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.bean.CarBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * ---接车信息
 */
public class GreetCartActivity extends BaseActivity implements View.OnClickListener {

    private EditText company_arrangement;//公司安排
    private EditText et_address;//接车地址
    private EditText et_station_name;//站名
    private TextView et_time;//到站时间
    private TextView select_cart;//车辆要求

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_greet_cart);

        initData();
        initTitle();
        company_arrangement = (EditText) findViewById(R.id.company_arrangement);
        et_address = (EditText) findViewById(R.id.et_address);
        et_station_name = (EditText) findViewById(R.id.et_station_name);
        et_time = (TextView) findViewById(R.id.et_time);
        et_time.setOnClickListener(this);
        select_cart = (TextView) findViewById(R.id.select_cart);
        select_cart.setOnClickListener(this);
        if (!need_info.getZhanming().toString().trim().equals("")) {
            et_time.setText(need_info.getArrive_time());
            et_station_name.setText(need_info.getZhanming());
            et_address.setText(need_info.getAddress());
            select_cart.setText(need_info.getCar_name());
            company_arrangement.setText(need_info.getAbout());
        }

    }

    private CarBean.InfoEntity info1 = new CarBean.InfoEntity();


    private ApplyMessagebean.NeedInfoEntity need_info = new ApplyMessagebean.NeedInfoEntity();

    private void initData() {
        need_info = ApplyMessageActivity.bean.getNeed_info();
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.greet_cart_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setVisibility(View.VISIBLE);
        submit.setText("保存");
        submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_time:

//                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
//                    @Override
//                    public void onTimeSelect(Date date, View v) {//选中事件回调
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String defaultTime = format.format(date);
//                        et_time.setText(defaultTime);
//                    }
//                })   //.setTitleSize(16)//标题文字大小
//                        // .setTitleText("预算系数")
//                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
//                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
//                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
//                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
//
//                        .setType(TimePickerView.Type.ALL)//默认全部显示
//                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
//                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
//                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
//                        .setLabel("年", "月", "日", "时", "分", "秒")
//                        .isDialog(true)//是否显示为对话框样式
//                        .build();
//                pvTime.show();

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                String[] b = str.split("-");
                Calendar startDate = Calendar.getInstance();
                startDate.set(Integer.parseInt(b[0]), Integer.parseInt(b[1]) - 1, Integer.parseInt(b[2]));
                Calendar endDate = Calendar.getInstance();
                endDate.set(2110, 10, 29);
                TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        String defaultTime = format.format(date);
                        et_time.setText(defaultTime);
                    }
                })
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .setType(TimePickerView.Type.YEAR_MONTH_DAY_HOUR_MIN)//默认全部显示
                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                        .setTitleBgColor(getResources().getColor(R.color.white))//标题背景颜色 Night mode
                        .setBgColor(getResources().getColor(R.color.white))//滚轮背景颜色 Night mode
                        .setRangDate(startDate, endDate)//默认是1960-2110年
                        .setDate(startDate)// 默认是系统时间*/
                        .build();
                pvTime.show();
                break;
            case R.id.select_cart:
                startActivityForResult(new Intent(this, AllCarActivity.class), 1);
                break;
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                String et_times = et_time.getText().toString().trim();
                String et_station_names = et_station_name.getText().toString().trim();
                String et_addresss = et_address.getText().toString().trim();
                String company_arrangements = company_arrangement.getText().toString().trim();
                if (et_times.equals("")) {
                    MyToast("请填写到站时间");
                    return;
                }
                if (et_station_names.equals("")) {
                    MyToast("请填写站名");
                    return;
                }
                if (et_addresss.equals("")) {
                    MyToast("请填写接车地址");
                    return;
                }
                if (company_arrangements.equals("")) {
                    MyToast("请填写公司安排");
                    return;
                }
                need_info.setArrive_time(et_times);
                need_info.setZhanming(et_station_names);
                need_info.setAddress(et_addresss);
                need_info.setAbout(company_arrangements);
                need_info.setIs_car("1");
                setResult(RESULT_OK);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    info1 = (CarBean.InfoEntity) data.getSerializableExtra("bean");
                    need_info.setCar_name(info1.getCar_name());
                    need_info.setCar_id(info1.getCar_number());
                    need_info.setIs_car(info1.getId());
                    select_cart.setText(need_info.getCar_name());
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
